package org.jeecgframework.web.controller;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.service.ZyInspectionRecordService;
import org.jeecgframework.web.system.pojo.base.ZyFileLogEntity;
import org.jeecgframework.web.system.pojo.base.ZyInspectionRecordEntity;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.web.util.FileBean;
import org.jeecgframework.web.util.StaticValue;
import org.jeecgframework.web.util.UploadFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;




/**   
 * @Title: Controller
 * @Description: 巡检记录
 * @author ZhangYunlong
 * @date 2016-05-17 18:38:21
 * @version V1.0   
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/zyInspectionRecordController")
public class ZyInspectionRecordController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ZyInspectionRecordController.class);

	@Autowired
	private ZyInspectionRecordService zyInspectionRecordService;
	@Autowired
	private SystemService systemService;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 巡检记录列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "zyInspectionRecord")
	public ModelAndView zyInspectionRecord(HttpServletRequest request) {
		return new ModelAndView("com/zybk/zy_inspection_record_import/zyInspectionRecordList");
	}
	
	/**
	 * easyui AJAX请求数据
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(ZyInspectionRecordEntity zyInspectionRecord,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(ZyInspectionRecordEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, zyInspectionRecord, request.getParameterMap());
		try{
		//自定义追加查询条件
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		this.zyInspectionRecordService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	
	/**
	 * 删除巡检记录
	 * 
	 * @return
	 */
	@RequestMapping(params = "doDel")
	@ResponseBody
	public AjaxJson doDel(ZyInspectionRecordEntity zyInspectionRecord, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		zyInspectionRecord = systemService.getEntity(ZyInspectionRecordEntity.class, zyInspectionRecord.getId());
		message = "巡检记录删除成功";
		try{
			zyInspectionRecordService.delete(zyInspectionRecord);
			systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		}catch(Exception e){
			e.printStackTrace();
			message = "巡检记录删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	
	/**
	 * 批量删除巡检记录
	 * 
	 * @return
	 */
	 @RequestMapping(params = "doBatchDel")
	@ResponseBody
	public AjaxJson doBatchDel(String ids,HttpServletRequest request){
		AjaxJson j = new AjaxJson();
		message = "巡检记录删除成功";
		try{
			for(String id:ids.split(",")){
				ZyInspectionRecordEntity zyInspectionRecord = systemService.getEntity(ZyInspectionRecordEntity.class, 
				id
				);
				zyInspectionRecordService.delete(zyInspectionRecord);
				systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
			}
		}catch(Exception e){
			e.printStackTrace();
			message = "巡检记录删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	
	 /**
	 * @author xiul
	 * 2016-6-17 下午3:53:32
	 * Description 巡检记录导入页面 跳转
	 */
	@RequestMapping(params = "uploadFile")
	public ModelAndView uploadFile(HttpServletRequest request) {
		return new ModelAndView("com/zybk/file_upload/uploadInspectionRecord");
	}

	 
	/**
	 * @author xiul
	 * 2016-6-29 上午11:23:13
	 * Description 保存 上传的文件 
	 */
	@ResponseBody
	@RequestMapping(params = "importExcel")
	public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
		AjaxJson j = new AjaxJson();
		FileBean fileBean = UploadFileUtil.UploadFile(request);
		ZyFileLogEntity fileLog  = new ZyFileLogEntity();
		fileLog.setBusinessType(StaticValue.IMPORT_FILE_INSPECTION_RECORD); //文件业务
		fileLog.setOldFileName(fileBean.getFileRealName());//文件原名
		fileLog.setNewFileName(fileBean.getFileName());//文件新名
		fileLog.setFilePath(fileBean.getSavePath());
		fileLog.setFileType("xlsx");
		fileLog.setStatus("0");
		systemService.save(fileLog);
		j.setObj(fileBean.getSavePath());
		return j;
	}
	
	/**
	 * @author xiul
	 * 2016-6-29 上午11:23:26
	 * Description 解析保存的excel 保存数据 
	 */
	@RequestMapping(params = "readExcel", method = RequestMethod.POST)
	@ResponseBody
	public void readExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
		System.out.println("进入解析excel 保存数据ACTION》》》》》");
		zyInspectionRecordService.parseExcel(request);
	}
	
}
